import {CodeUtil} from "../../codeAdapter/CodeUtil";
import MathUtil from "../../codeAdapter/MathUtil";

export class LocModel {
    x: number;
    y: number;
    z: number;

    constructor(x: number, y: number, z?: number) {
        this.x = x;
        this.y = y;
        this.z = z;
    }

    public getAngle(loc: LocModel) {
        const angle = MathUtil.atan2(loc.y - this.y, loc.x - this.x) * 180 / MathUtil.getPI();
        return angle;
    }
    public getAbsDistance(loc: LocModel) {
        const x1 = this.x;
        const y1 = this.y;
        const x2 = loc.x;
        const y2 = loc.y;
        const disX = MathUtil.abs(x1 - x2);
        const disY = MathUtil.abs(y1 - y2);
        const distance2 = disX * disX + disY * disY;
        const distance = MathUtil.sqrt(distance2);
        return distance;
    }
    public offset(distance: number, angle: number) {
        const radian = angle * MathUtil.getPI() / 180; // 将角度转换成弧度
        const newX = this.x + distance * MathUtil.cos(radian);
        const newY = this.y + distance * MathUtil.sin(radian);
        return new LocModel(newX, newY);
    }
    public isVaild() {
        return ((!CodeUtil.isNullOrZero(this.x)) && (!CodeUtil.isNullOrZero(this.y)))
    }
}